package com.ecut.property_project.controller.base;

import com.ecut.property_project.model.base.BaseCompany;
import com.ecut.property_project.model.base.BaseDept;
import com.ecut.property_project.service.base.BaseCompanyService;
import com.ecut.property_project.service.base.BaseDeptService;
import com.ecut.property_project.utils.Page;
import com.ecut.property_project.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/basedept")
public class BaseDeptController {
    @Autowired
    private BaseDeptService service;

    @PostMapping
    Result insertSelective(@RequestBody BaseDept record) {
        service.insert(record);
        return Result.succ("部门信息添加成功");
    }

    @DeleteMapping("/{id}")
    Result deleteByPrimaryKey(@PathVariable int id) {
        service.deleteByPrimaryKey(id);
        return Result.succ("删除成功");
    }

    @PutMapping
    Result updateByPrimaryKeySelective(@RequestBody BaseDept record) {
        service.updateByPrimaryKeySelective(record);
        return Result.succ("修改成功");
    }

    @GetMapping("/{id}")
    Result selectByPrimaryKey(@PathVariable int id) {
        BaseDept record = service.selectByPrimaryKey(id);
        return Result.succ(record);
    }

    @GetMapping
    Result selectAll() {
        List<BaseDept> record = service.selectAll();
        return Result.succ(record);
    }

    @GetMapping("/selectbypage")
    Result selectByPage(int page, int pageSize) {
        Page<BaseDept> objectPage = new Page<>();

        int sum = service.selectAll().size();
        List<BaseDept> record = service.selectByPage(page-1, pageSize);

        objectPage.setPage(page);
        objectPage.setPageSize(pageSize);
        objectPage.setTotal(sum);
        objectPage.setData(record);
        return Result.succ(objectPage);
    }

}
